import {
  Controller,
  Get,
  Post,
  Body,
  Put,
  Param,
  Delete,
  UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

import { info } from './mock/user';

import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { AuthService } from '../../auth/auth.service';

@Controller('user')
export class UserController {
  constructor(
    private readonly authService: AuthService,
    private readonly userService: UserService,
  ) {}

  @Post()
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  // @UseGuards(AuthGuard('jwt'))
  @Post('register')
  async register(@Body() body: any) {
    return await this.userService.register(body);
  }

  @Post('login')
  async login(@Body() loginBody: any) {
    console.log('JWT验证 - Step 1: 用户请求登录');
    const authResult = await this.authService.validateUser(
      loginBody.username,
      loginBody.password,
    );

    switch (authResult.code) {
      case 1:
        return this.authService.certificate(authResult.user);

      case 2:
        return {
          code: 600,
          msg: '账号或密码不正确',
        };

      default:
        return {
          code: 600,
          msg: '查无此人',
        };
    }
  }

  @Get()
  findAll() {
    return this.userService.findAll();
  }
  @Get('info')
  getUserInfo() {
    return {
      code: 200,
      msg: '用户基本信息和权限信息',
      data: info(''),
    };
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    // return this.userService.findOne(+id);
  }

  @Put(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(+id, updateUserDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.userService.remove(+id);
  }
}
